package com.hnichr.ump.plan.controller;


import com.hnichr.ump.common.plan.entity.PlanGroupUser;
import com.hnichr.ump.common.plan.service.PlanGroupUserService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plan/groupuser/v1/")
@Tag(name = "PlanGroupUserController", description = "重点工作角色用户接口")
public class PlanGroupUserController {

    @Autowired
    private PlanGroupUserService planGroupUserService;

    @GetMapping("list")
    @Operation(summary = "重点工作角色用户列表", description = "获取重点工作角色用户列表（无分页）", method = "GET")
    public Result<List<PlanGroupUser>> list(@Parameter(description="查询参数") Criteria criteria){
        List<PlanGroupUser> list = planGroupUserService.list(PlanGroupUser.class,criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "重点工作角色用户列表", description = "重点工作角色用户列表（分页）", method = "GET")
    public Result<Page<PlanGroupUser>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<PlanGroupUser> list = planGroupUserService.listPage(PlanGroupUser.class,page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取重点工作角色用户", description = "根据ID获取重点工作角色用户", method = "GET")
    public Result<PlanGroupUser> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        PlanGroupUser groupUser = planGroupUserService.getById(PlanGroupUser.class,id);
        return Result.OK(groupUser);
    }

    @GetMapping("listByGroup/{groupId}")
    @Operation(summary = "根据groupId获取重点工作角色用户", description = "根据groupId获取重点工作角色用户", method = "GET")
    public Result<List<PlanGroupUser>> listByGroupId(@Parameter(description="groupId") @PathVariable(name = "groupId", required = true) String groupId) {
        List<PlanGroupUser> groupUsers = planGroupUserService.listByGroupId(groupId);
        return Result.OK(groupUsers);
    }

    @GetMapping("pageByGroup/{groupId}")
    @Operation(summary = "根据groupId获取重点工作角色用户", description = "根据groupId获取重点工作角色用户", method = "GET")
    public Result<Page<PlanGroupUser>> pageByGroupId(@Parameter(description="groupId") @PathVariable(name = "groupId", required = true) String groupId,@Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<PlanGroupUser> groupUsers = planGroupUserService.pageByGroupId(groupId,page);
        return Result.OK(groupUsers);
    }

    @PostMapping("add")
    @Operation(summary = "新增重点工作角色用户", description = "新增重点工作角色用户", method = "POST")
    public Result<List<PlanGroupUser>> add(@RequestBody List<PlanGroupUser> planGroupUsers) {
        try{
            planGroupUserService.add(planGroupUsers);
            return Result.OK(planGroupUsers);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除重点工作角色用户", description = "根据id删除重点工作角色用户", method = "DELETE")
    public Result<PlanGroupUser> remove(@PathVariable(name = "ids", required = true) String ids) {
        planGroupUserService.remove(ids);
        return Result.OK();
    }
}
